package com.koron.css2.clientCenter.impl;

import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.bean.UserStopBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.StopUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.bean.MsChgPlanBean;
import com.koron.css2.meterService.bean.MsChgPlanDetailBean;
import com.koron.css2.meterService.mapper.MsChgPlanDetailMapper;
import com.koron.css2.meterService.mapper.MsChgPlanMapper;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.text.SimpleDateFormat;

/**
 * 停用审批处理
 * 
 * @author 甄玉晴
 *
 */
public class StopUserWorkFlow implements ProcessInterface {

	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
		MessageBean<?> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Void.class);
		try {
			// 审批后执行
			if("approval".equalsIgnoreCase(fromNodeCode) && 32 == operation) {
				// 获取停用的单据信息
				StopUserMapper mapper = factory.getMapper(StopUserMapper.class);
				UserStopBean bean = mapper.getUserStopInfoById(billId);
				// 修改用户信息
				UserInfoMapper userMapper = factory.getMapper(UserInfoMapper.class);
				userMapper.userInfoStopUpdate(bean);
				//获取用户信息
				UserBean user=userMapper.getUserInfoByUserNo(bean.getUserNo(), UserBean.USER);
				//更新变更事项到正式表
				ChangeUserMapper cmapper = factory.getMapper(ChangeUserMapper.class);
				cmapper.userChangeItemfromTemp(billId);
				//如果需要拆表，则生成一张拆表单据
				if(bean.getSplitMeterFlag()==1) {
					//填写拆表单据
					MsChgPlanBean msBean=addMsChgPlan(factory,user,bean,userInfo);
					//启用水表复表流程
					/*
					ProcessStartBean processStart = new ProcessStartBean();
					processStart.setBillId(msBean.getId());
					processStart.setTitle("拆表审批"+"("+msBean.getBillNo()+")");
					processStart.setOperator(userInfo.getUserInfo().getAcount());
					processStart.setFormVariables(new HashMap<String, Object>());
					processStart.setComment("拆表登记");
					processStart.setReceiptType("ms_chg_plan");
					*/
					/*JSONObject json = new JSONObject();
					json.put("title", "拆表审批"+"("+msBean.getBillNo()+")");
					json.put("billId", msBean.getId());
					json.put("operator", userInfo.getUserInfo().getAcount());
					json.put("formVariables", new HashMap<String, Object>());
					json.put("comment", "拆表审批");
					json.put("receiptType", "ms_chg_plan#2");
					
					
					RequestBean req = new RequestBean();
					req.setData(json);				
					ProcessCommit processCommit = new ProcessCommit();
					info = processCommit.exec(factory, userInfo, req);*/
				}				
			}
		} catch (Exception e) {
			factory.close(false);
			logger.error("停用审批后续操作失败", e);
			info = MessageBean.create(Constant.MESSAGE_INT_FAIL, "停用审批后续操作失败", Void.class);
		}
		return info;
	}
	/**
	 * 生成拆表单据
	 * @param factory
	 * @param user
	 * @param stopBean
	 * @param userInfo
	 */
	public MsChgPlanBean addMsChgPlan(SessionFactory factory,UserBean user,UserStopBean stopBean,UserInfoBean userInfo) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		//获取拆表信息
		MsChgPlanBean bean=new MsChgPlanBean();
		try {
			bean.setBillDate(simpleDateFormat.parse(stopBean.getBillDate()));
			bean.setBillNo(Tools.getBillNo(factory, "MS_CHG_PLAN"));
			bean.setReceiptType(2);
			bean.setReplaceType(Integer.valueOf(stopBean.getSplitMeterWay()));
			bean.setReplaceReason(stopBean.getSplitMeterRemark());
			bean.setMeterAmount(1);
			bean.setBackfillAmount(0);
			bean.setCreateInfo(userInfo);
			MsChgPlanMapper msMapper=factory.getMapper(MsChgPlanMapper.class);
			//添加单据信息
			msMapper.addMsChgPlan(bean);
			//添加明细表
			MsChgPlanDetailBean detail=new MsChgPlanDetailBean();
			detail.setReceiptId(bean.getId());
			detail.setMargin(0);
			detail.setUserNo(user.getUserNo());
			detail.setOldMeterNo(user.getMeterNo());
			detail.setUpperNum(user.getLastReadingNum());
			detail.setReplaceDate(simpleDateFormat.parse(stopBean.getStopDate()));
			detail.setCreateInfo(userInfo);
			MsChgPlanDetailMapper detailMapper=factory.getMapper(MsChgPlanDetailMapper.class);
			detailMapper.addMsChgPlanDetail(detail);
		} catch (Exception e) {
			logger.error("过户审批后续操作增加拆表单据失败", e);
			e.printStackTrace();
		}
		return bean;
	}
	@Override
	public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
		return exec(factory, userInfo, billId, fromNodeCode, operation);
	}

	@Override
	public MessageBean<?> after(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, String toNodeCode) {
		return null;
	}

}
